package com.pkutech.carcasher.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	public static final String FORMAT_DATE_DASH = "yyyy-MM-dd";

	public static final String FORMAT_DATE_WORD = "yyyy年MM月dd日";
	
	public static final String FORMAT_DATE_TIME = "yyyyMMddHHmmssSSS";

	private static int val = -1;

	private static int curval = 0;

	/**
	 * 获取当前日期格式字符串 , 格式自定义
	 * 
	 * @param formatString
	 *            格式字符串 如: yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNow(String formatString) {
	 
		if (StringUtils.isEmptyOrWhitespaceOnly(formatString)) {
			formatString = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat formatter_f = new SimpleDateFormat(formatString);
		Date currentTime_f = new Date(); // 得到当前系统时间
		String new_date_f = formatter_f.format(currentTime_f); // 将日期时间格式化
		return new_date_f;

	}

	public static String getTimeSS(Date date) {
		SimpleDateFormat formatter_f = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String str = formatter_f.format(date);
		return str;
	}

	public static String getTime(Date date) {
		SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd");
		String str = formatter_f.format(date);
		return str;
	}

	/**
	 * 计算本月共多少天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getCountDayForMonth(int year, int month) {
		if (month > 12)
			return 0;
		int[] monDays = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
				30, 31 };
		if (((year) % 4 == 0 && (year) % 100 != 0) || (year) % 400 == 0)
			monDays[2]++;

		return monDays[month];

	}

	/**
	 * 获取当前日期的字符串格式 2010-11-12
	 * 
	 * @return
	 */
	public static String getNow() {
		return getNow("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 返回时间的毫秒值
	 * 
	 * @return
	 */
	public static long nowTimeInMillis() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());

		return cal.getTimeInMillis();

	}

	/**
	 * 得到当天 零点
	 * 
	 * @param date
	 * @return
	 */
	public static Date getZero(Date date) {
		SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd");

		String new_date_f = formatter_f.format(date); // 将日期时间格式化
		new_date_f = new_date_f + " 00:00:00";
		return parseDate(null, new_date_f);
	}

	/**
	 * 得到当天 深夜(午夜12点)
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMidnight(Date date) {
		SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd");

		String new_date_f = formatter_f.format(date); // 将日期时间格式化
		new_date_f = new_date_f + " 23:59:59";
		return parseDate(null, new_date_f);
	}

	/**
	 * 获取指定日期月份的最后一天
	 * 
	 * @param date
	 *            日期对象
	 * @return
	 */
	public static Date getLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		return cal.getTime();
	}

	/**
	 * 取得指定日期N天后的日期
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}

	/**
	 * 计算两个日期之间相差的天数,严格,计算相差秒数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}
	
	/**
	 * 计算两个时间相差的天数
	 * @author
	 * ***/
	public static Long getDays(String beginTime,String endTime) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long day =0l;
		try {
			Date beginDate = df.parse(beginTime);
			Date endDate = df.parse(endTime);
			long diff = endDate.getTime()-beginDate.getTime();
			day = diff/(1000*60*60*24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * 计算两个日期之间相差的天数,只关心天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long daysOfTwo(Date fDate, Date oDate) {

		Calendar aCalendar = Calendar.getInstance();
		 
		aCalendar.setTime(fDate);
		Calendar bCalendar = Calendar.getInstance();
		bCalendar.setTime(oDate);

		long atimeInMillis = aCalendar.getTimeInMillis();
		long btimeInMillis = bCalendar.getTimeInMillis();
		long day = ( btimeInMillis-atimeInMillis)/(24*60*60*1000);
		return  day;

	}

	/**
	 * @param startDay
	 *            需要比较的时间 不能为空(null),需要正确的日期格式 ,如：2009-09-12
	 * @param endDay
	 *            被比较的时间 为空(null)则为当前时间
	 * 
	 * @return 举例： compareDate("2009-09-12", null );//比较天
	 *         compareDate("2009-09-12", null );//比较月 compareDate("2009-09-12",
	 *         null );//比较年
	 */
	public static int compareDate(Date startDay, Date endDay) {
		boolean issx = startDay.after(endDay);
		int n = 0;
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		Date[] ds = exchangeDate(startDay, endDay);
		c1.setTime(ds[0]);
		c2.setTime(ds[1]);
		while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果
			c1.add(Calendar.DATE, 1);
			if (issx) {
				n++;
			} else {
				n--;
			}
		}
		return n;
	}

	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 *            需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String format(Date aTs_Datetime) {
		return format(aTs_Datetime, "yyyy-MM-dd");
	}

	/**
	 * 将Date类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 * @param as_Pattern
	 * @return
	 */
	public static String format(Date aTs_Datetime, String as_Pattern) {
		if (aTs_Datetime == null || as_Pattern == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Pattern);

		return dateFromat.format(aTs_Datetime);
	}

	/**
	 * Parse a string and return the date value in the specified format
	 * 
	 * @param strFormat
	 * @param dateValue
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 */
	public static Date parseDate(String strFormat, String dateValue) {
		if (dateValue == null)
			return null;

		if (strFormat == null) {
			strFormat = "yyyy-MM-dd HH:mm:ss";
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			newDate = null;
		}

		return newDate;
	}

	/**
	 * 比较日期先后,并摆正顺序 不是引用类型?函数外又还原...
	 */
	public static Date[] exchangeDate(Date startDate, Date endDate) {
		Date datetemp;
		if (startDate.after(endDate)) {
			datetemp = endDate;
			endDate = startDate;
			startDate = datetemp;
		}
		return new Date[] { startDate, endDate };
	}

	/**
	 * 传入时间字符串,加一天后返回Date
	 * 
	 * @param date
	 *            时间 格式 YYYY-MM-DD
	 * @return
	 */
	public static Date addDate(String date) {
		if (date == null) {
			return null;
		}

		Date tempDate = parseDate("yyyy-MM-dd", date);
		String year = format(tempDate, "yyyy");
		String month = format(tempDate, "MM");
		String day = format(tempDate, "dd");
		GregorianCalendar calendar = new GregorianCalendar(
				Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));

		calendar.add(GregorianCalendar.DATE, 1);
		return calendar.getTime();
	}

	/**
	 * 获得昨天时间，格式自定义
	 * 
	 * @param format
	 * @return
	 */
	public static String getYesterdayDate(String format) {
		Calendar day = Calendar.getInstance();
		day.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(day.getTime());
		return date;
	}
	/**
	 * 获得时间，格式自定义
	 *
	 * @param format
	 * @return
	 */
	public static String getThisDate(String format,Date lastDate,int days) {
		Calendar day = Calendar.getInstance();
		day.setTime(lastDate);
		day.add(Calendar.DATE, days);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(day.getTime());
		return date;
	}
	/**
	 * 获得时间，格式自定义
	 *
	 * @param format
	 * @return
	 */
	public static Date getAfterDate(String format,Date lastDate,int days) {
		Calendar day = Calendar.getInstance();
		day.setTime(lastDate);
		day.add(Calendar.DATE, days);

		return day.getTime();
	}
	/**
	 * 实现给定某日期，判断是星期几 date:必须yyyy-MM-dd格式
	 */
	public static String getWeekday(String date) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdw = new SimpleDateFormat("E");
		Date d = null;
		try {
			d = sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			d = new Date();
		}
		return sdw.format(d);
	}
	/**
	 * 实现给定某日期，判断是星期几 date:必须yyyy-MM-dd格式
	 */
	public static int getWeekday2(String date) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
		Date d;
		try {
			d=sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			d = new Date();
		}
		cal.setTime(d);
		int week_index = cal.get(Calendar.DAY_OF_WEEK);
		return week_index;
	}
	/**
	 * 获得当前日期与本周一相差的天数
	 */
	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek == 1) {
			return -6;
		} else {
			return 2 - dayOfWeek;
		}
	}

	/**
	 * 获取某年某月第一天是星期几,星期天为0,星期六为6
	 * 
	 * @return
	 */
	public static int getFirstDayWeek(int year, int month) {
		String wd = DateUtils.getWeekday(year + "-" + month + "-1");

		if ("星期日".equals(wd) || "SUNDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 0;
		}
		if ("星期一".equals(wd) || "MONDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 1;
		}
		if ("星期二".equals(wd) || "TUESDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 2;
		}
		if ("星期三".equals(wd) || "Wednesday".equalsIgnoreCase(wd.toUpperCase())) {
			return 3;
		}
		if ("星期四".equals(wd) || "THURSDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 4;
		}
		if ("星期五".equals(wd) || "FRIDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 5;
		}
		if ("星期六".equals(wd) || "SATURDAY".equalsIgnoreCase(wd.toUpperCase())) {
			return 6;
		}

		return 0;

	}

	/**
	 * 根据当前时间,返回一个顺序的数字字符串
	 * 
	 * @return
	 */
	public static synchronized String getTransactionIdByTime() {
		String st = String.valueOf(System.currentTimeMillis());
		curval = val + 1;
		val = curval;
		if (curval < 10) {
			return st + "00" + String.valueOf(curval);
		} else if (curval < 100) {
			return st + "0" + String.valueOf(curval);
		} else {
			if (curval < 999) {
				return st + String.valueOf(curval);
			} else {
				val = -1;
				return st + String.valueOf(curval);
			}
		}
	}

	public static String timestamp() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(new Date());
	}

	public static void main(String[] args) {
		getFirstDayWeek(2011, 5);
	}

	/**
	 * 获得本周星期一的日期
	 */
	public static String getCurrentMonday(String format) {

		int mondayPlus = getMondayPlus();
		Calendar currentDate = Calendar.getInstance();
		currentDate.add(Calendar.DATE, mondayPlus);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(currentDate.getTime());
		return date;
	}

	/**
	 * 获得上周星期一的日期
	 */
	public static String getPreviousMonday(String format) {

		int mondayPlus = getMondayPlus();
		Calendar currentDate = Calendar.getInstance();
		currentDate.add(GregorianCalendar.DATE, mondayPlus - 7);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(currentDate.getTime());
		return date;
	}

	/**
	 * 获得下周星期一的日期
	 */
	public static String getNextMonday(String format) {

		int mondayPlus = getMondayPlus();
		// GregorianCalendar currentDate = new GregorianCalendar();
		Calendar currentDate = Calendar.getInstance();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(currentDate.getTime());
		return date;
	}

	/**
	 * 按日加
	 * 
	 * @param value
	 * @return
	 */
	public static Date addDay(int value) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.DAY_OF_YEAR, value);
		return now.getTime();
	}

	/**
	 * 按日加,指定日期
	 * 
	 * @param date
	 * @param value
	 * @return
	 */
	public static Date addDay(Date date, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.DAY_OF_YEAR, value);
		return now.getTime();
	}

	/**
	 * 按月加
	 * 
	 * @param value
	 * @return
	 */
	public static Date addMonth(int value) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MONTH, value);
		return now.getTime();
	}

	/**
	 * 按月加,指定日期
	 * 
	 * @param date
	 * @param value
	 * @return
	 */
	public static Date addMonth(Date date, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.MONTH, value);
		return now.getTime();
	}

	/**
	 * 按年加
	 * 
	 * @param value
	 * @return
	 */
	public static Date addYear(int value) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.YEAR, value);
		return now.getTime();
	}

	/**
	 * 按年加,指定日期
	 * 
	 * @param date
	 * @param value
	 * @return
	 */
	public static Date addYear(Date date, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.YEAR, value);
		return now.getTime();
	}

	/**
	 * 按小时加
	 * 
	 * @param value
	 * @return
	 */
	public static Date addHour(int value) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.HOUR_OF_DAY, value);
		return now.getTime();
	}

	/**
	 * 按小时加,指定日期
	 * 
	 * @param date
	 * @param value
	 * @return
	 */
	public static Date addHour(Date date, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.HOUR_OF_DAY, value);
		return now.getTime();
	}

	/**
	 * 按分钟加
	 * 
	 * @param value
	 * @return
	 */
	public static Date addMinute(int value) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MINUTE, value);
		return now.getTime();
	}

	/**
	 * 按分钟加,指定日期
	 * 
	 * @param date
	 * @param value
	 * @return
	 */
	public static Date addMinute(Date date, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.MINUTE, value);
		return now.getTime();
	}

	/**
	 * 年份
	 * 
	 * @return
	 */
	public static int year() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.YEAR);
	}

	/**
	 * 月份
	 * 
	 * @return
	 */
	public static int month() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MONTH) + 1;
	}

	/**
	 * 日(号)
	 * 
	 * @return
	 */
	public static int day() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 小时(点)
	 * 
	 * @return
	 */
	public static int hour() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 分钟
	 * 
	 * @return
	 */
	public static int minute() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MINUTE);
	}

	/**
	 * 秒
	 * 
	 * @return
	 */
	public static int second() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.SECOND);
	}

	/**
	 * 星期几(礼拜几)
	 * 
	 * @return
	 */
	public static int weekday() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 是上午吗?
	 * 
	 * @return
	 */
	public static boolean isAm() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.AM_PM) == 0;
	}

	/**
	 * 是下午吗?
	 * 
	 * @return
	 */
	public static boolean isPm() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.AM_PM) == 1;
	}

	/**
	 * 获取某年某月第一天是星期几,星期天为0,星期六为6
	 * 
	 * @return
	 */
	public static int parseWeek(String weekString) {

		if ("星期日".equals(weekString)
				|| "SUNDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 0;
		}
		if ("星期一".equals(weekString)
				|| "MONDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 1;
		}
		if ("星期二".equals(weekString)
				|| "TUESDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 2;
		}
		if ("星期三".equals(weekString)
				|| "Wednesday".equalsIgnoreCase(weekString.toUpperCase())) {
			return 3;
		}
		if ("星期四".equals(weekString)
				|| "THURSDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 4;
		}
		if ("星期五".equals(weekString)
				|| "FRIDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 5;
		}
		if ("星期六".equals(weekString)
				|| "SATURDAY".equalsIgnoreCase(weekString.toUpperCase())) {
			return 6;
		}

		return 0;
	}
	/**
	 * 获取时间间隔多少天 3.5=4
	 *
	 * @return
	 */
	public static long getDays(String startTime, String endTime, String format) {
		//按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nd = 1000*60*60*24;//一小时的毫秒数
		//获得两个时间的毫秒时间差异
		long diff=0;
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		long days = diff/nd;//计算差多少天
		System.out.println("时间相差天数："+days);
		return  days;
	}
	/**
	 * 获取时间间隔多少小时 3.5=4
	 *
	 * @return
	 */
	public static long getHours(String startTime, String endTime, String format) {
		//按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nh = 1000*60*60;//一小时的毫秒数
		//获得两个时间的毫秒时间差异
		long diff=0;
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		long hour = diff/nh+1;//计算差多少小时
		System.out.println("时间相差小时数："+hour);
		return  hour;
	}
	/**
	 * 获取时间间隔多少分钟 3.5=4
	 *
	 * @return
	 */
	public static long getMinites(String startTime, String endTime, String format) {
		//按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nm = 1000*60;//一分钟的毫秒数
		//获得两个时间的毫秒时间差异
		long diff=0;
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		long min = diff/nm+1;//计算差多少分钟
		System.out.println("时间相差分钟数："+min);
		return  min;
	}

}
